<template>
  <el-card class="mt-20" shadow="hover">
    <el-button
      type="primary"
      :icon="Plus"
      @click="showAddAttr"
      v-permission="'btn.Attr.add'"
      >添加属性</el-button
    >
    <el-table :data="attrList" border class="mt-20">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="属性名称" prop="attrName" width="180" />
      <el-table-column label="属性值列表">
        <template #default="{ row }">
          <el-tag
            class="mr-5"
            type="success"
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            >{{ attrValue.valueName }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            class="mr-10"
            title="修改属性"
            @click="showUpdateAttr(row)"
            v-permission="'btn.Attr.update'"
          ></el-button>
          <el-popconfirm
            :title="`你确定要删除 ${row.attrName}吗?`"
            @confirm="delAttr(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :icon="Delete"
                title="删除属性"
                v-permission="'btn.Attr.remove'"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { watch, ref } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
import { getAttrListApi, delAttrApi } from "@/api/product/attr";
import { AttrList } from "@/api/product/model/attrModel";
import { useAttrStore } from "@/stores/attr";
import type { AttrItem } from "@/api/product/model/attrModel";
import { ElMessage } from "element-plus";

// 点击删除
const delAttr = async (id: number) => {
  await delAttrApi(id);
  ElMessage.success("删除属性成功~");
  getAttrList();
};
const emit = defineEmits(["setIsShowAttrList"]);
// 点击添加，显示添加组件
const showAddAttr = () => {
  emit("setIsShowAttrList", false);
  // attrStore.attr = {
  //   attrName: "",
  //   attrValueList: [],
  // };
  attrStore.$reset(); // 重新pinia这个模块数据，为初始化值
};
// 点击修改，显示修改组件
const attrStore = useAttrStore();
const showUpdateAttr = (row: AttrItem) => {
  emit("setIsShowAttrList", false);
  // pinia管理attr数据
  attrStore.attr = row;
};
const categoryStore = useCategoryStore();
const attrList = ref<AttrList>();
// 获取商品属性列表
const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;
  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

watch(
  // 监听category3Id有值，发送获取商品属性列表请求，更新响应式数据attrList
  () => categoryStore.category3Id,
  async (category3Id) => {
    // 如果category3Id为undefined不发请求
    if (!category3Id) return;
    getAttrList();
  },
  {
    immediate: true, // 开启立即监视 问题：在添加属性后跳回没有数据，相当于初始化挂载，需要开启立即监视
  }
);
</script>

<style scoped></style>
